package servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import java.sql.Connection;

import javabean.Admin;
import javabean.Base;
import net.sf.json.JSONObject;

/**
 * Servlet implementation class BookAdd
 */
@WebServlet("/admin/postmanAdd")
public class PostmanAdd extends HttpServlet {
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setContentType("application/json; charset=utf8");
		HttpSession session = req.getSession();
		String adminid = session.getAttribute("admin").toString();
		Admin admin = new Admin();
		String area = null;
		try {
			area = admin.area(adminid);
		} catch (ClassNotFoundException e) {
			throw new RuntimeException(e);
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
		String id =req.getParameter("pid");
		String name = req.getParameter("name");
		String psw = req.getParameter("psw");
		String phonenumber = req.getParameter("phonenumber");
		System.out.println("-------------");			//debug
		JSONObject json = new JSONObject();
		Connection connection = null;
		PreparedStatement pstmt = null;
		//ResultSet resultSet = null;
		int result = 0;
		String sql = "insert into postman(Pid,Pname, Ppassword, areaid,Pphonenumber) values(?,?,?,?,?)";
		System.out.println(sql);
		PrintWriter out = resp.getWriter();
		try {
			connection = (Connection) Base.getConnection();
			pstmt = connection.prepareStatement(sql);
			pstmt.setString(1, id);
			pstmt.setString(2, name);
			pstmt.setString(3, psw);
			pstmt.setString(4, area);
			pstmt.setString(5, phonenumber);
			result = pstmt.executeUpdate();
		} catch (SQLException e) {
			
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}finally {
			try {
				Base.closeResource(connection, pstmt, null);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
//		sql = "CREATE TABLE `"+id+"delivery` ( `id` int(10)  unsigned zerofill NOT NULL,  `nextstep` varchar(20) DEFAULT NULL, `state` enum('0','1') DEFAULT NULL, PRIMARY KEY (`id`), CONSTRAINT `"+id+"delivery_ibfk_1` FOREIGN KEY (`id`) REFERENCES `oorder` (`Oid`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;";
		sql ="CREATE TABLE `"+id+"delivery` ( `Oid` int(10) unsigned zerofill NOT NULL AUTO_INCREMENT, `destination` varchar(20) DEFAULT NULL, `detail` varchar(100) DEFAULT NULL, `Uid` varchar(20) DEFAULT NULL, `nowarea` varchar(10) DEFAULT NULL, `state` varchar(20) DEFAULT NULL,`time` time DEFAULT NULL, `Rid` varchar(20) DEFAULT NULL, PRIMARY KEY (`Oid`), KEY `Uid` (`Uid`), KEY `Rid` (`Rid`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3";
		System.out.println(sql);
		try {
			connection = (Connection) Base.getConnection();
			pstmt = connection.prepareStatement(sql);
			if(pstmt.executeUpdate()==0){
				result+=1;
			}
			System.out.println(result);
		} catch (Exception e) {
			e.printStackTrace();
		}

		if(result==2) {
			json.put("code", "0");
			json.put("msg", "success");
		}else {
			json.put("code", "1");
			json.put("msg", "error");
		}
		out.write(json.toString());
	}

}
